<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<div class="col-xs-12">
    <div class="box layui_mytool" style="float:left;">
        <div style="width: 100%;">
            <div class="table_left">
                <div style="float: left;">
                    <i class="glyphicon glyphicon-ok-sign"></i>&nbsp;已分配权限
                </div>
                <div style="float: right">
                    <a href="javascript:;" class="layui-btn layui-btn-sm layui-btn-normal" onclick="maneyAddOrRemoveAuth(1)">批量操作</a>
                </div>
            </div>
            <div class="table_right">
                <div style="float: left;">
                    <i class="glyphicon glyphicon-remove-sign"></i>&nbsp;未分配权限
                </div>
                <div style="float: right">
                    <a href="javascript:;" class="layui-btn layui-btn-sm layui-btn-normal" onclick="maneyAddOrRemoveAuth(2)">批量操作</a>
                </div>
            </div>
        </div>
        <div style="width: 100%;">
            <div class="table_left">
                <table class="layui-table" id="TbAuth1" lay-size="sm" lay-filter="ToolAuth1"></table>

            </div>
            <div class="table_right">
                <table class="layui-table" id="TbAuth2" lay-size="sm" lay-filter="ToolAuth2"></table>
            </div>
        </div>
    </div>
</div>
<input type="hidden" id="roleId" value="${param.roleId}">
<script>
    var tableIns1;
    var tableRender1;
    var tableIns2;
    var tableRender2;
    $(document).ready(function () {
        initTable1();
        initTable2();
    });

    //左table
    function initTable1() {
        layui.use(['table'], function () {
            tableIns1 = layui.table;
            tableRender1 = tableIns1.render({
                id: 'TbAuth1',
                elem: '#TbAuth1',
                height: 'full-70',
                url:"<c:url value="/web/admin/systemAuthority/getSystemAuthorityListJSON.json"/>",
                where: {
                    roleId: '${param.roleId}',
                    type: 2
                },
                method: 'post',
                limits: [20, 50, 100, 200],
                limit: 20,
                loading: true,
                page: true,
                request: {
                    pageName: 'page',
                    limitName: 'rows'
                },
                response: {
                    statusName: 'errcode',
                    statusCode: 0,
                    msgName: 'errmsg',
                    countName: 'total',
                    dataName: 'rows'
                },
                cols: [[
                    { checkbox: true, fixed: true },
                    {field:'authorityName','title': '权限名称',align:'center'},
                    {field:'authorityDescription','title': '权限描述',align:'center'},
                    {
                        field: 'title',
                        width: 70,
                        fixed: 'right',
                        title: '操作',
                        align: 'center',
                        templet: function (row) {
                            var buttonView = '&nbsp;' +
                                '<a class="layui-btn layui-btn-xs layui-btn-primary" href="javascript:void(0)" ' +
                                'onclick="addOrRemoveAuth(\'' + row.authorityId + '\',1)"><i class="glyphicon glyphicon-arrow-right"></i></a>' +
                                '&nbsp;';
                            return buttonView;
                        }
                    }
                ]]
            });
        });
    }

    //右table
    function initTable2() {
        layui.use(['table'], function () {
            tableIns2 = layui.table;
            tableRender2 = tableIns2.render({
                id: 'TbAuth2',
                elem: '#TbAuth2',
                height: 'full-70',
                url:"<c:url value="/web/admin/systemAuthority/getSystemAuthorityListJSON.json"/>",
                where: {
                    roleId: '${param.roleId}',
                    type: 1
                },
                method: 'post',
                limits: [20, 50, 100, 200],
                limit: 20,
                loading: true,
                page: true,
                request: {
                    pageName: 'page',
                    limitName: 'rows'
                },
                response: {
                    statusName: 'errcode',
                    statusCode: 0,
                    msgName: 'errmsg',
                    countName: 'total',
                    dataName: 'rows'
                },
                cols: [[
                    { checkbox: true, fixed: true },
                    {
                        field: 'title',
                        width: 70,
                        fixed: 'left',
                        title: '操作',
                        align: 'center',
                        templet: function (row) {
                            var buttonView = '&nbsp;' +
                                '<a class="layui-btn layui-btn-xs layui-btn-primary" href="javascript:void(0)" ' +
                                'onclick="addOrRemoveAuth(\'' + row.authorityId + '\',2)"><i class="glyphicon glyphicon-arrow-left"></i></a>' +
                                '&nbsp;';
                            return buttonView;
                        }
                    },
                    {field:'authorityName','title': '权限名称',align:'center'},
                    {field:'authorityDescription','title': '权限描述',align:'center'}
                ]]
            });
        });
    }

    function addOrRemoveAuth(authorityId, type) {
        $.ajax({
            type: 'POST',
            url: '<c:url value="/web/admin/systemRole/addOrRemoveAuthFromRole.action"/>',
            data: {
                roleId: $("#roleId").val(),
                authId: authorityId,
                type:type
            },
            async: false,
            success: function (result) {
                var errcode = result.errcode;
                var errmsg = result.errmsg;
                if (errcode == 0) {
                    tableIns.reload('TbAppliances');
                    parent.layer.msg('操作成功', {icon: 1});
                } else {
                    parent.layer.msg('操作失败', {shift: 6});
                }
                searchSubmit1();
                searchSubmit2();
            },
            error: function () {
                alert('ajax error!');
            }
        });
    }

    function maneyAddOrRemoveAuth(type) {
        var authArray = new Array();
        var checkStatus;
        if (type == 1) {
            checkStatus = tableIns1.checkStatus('TbAuth1');
        } else if (type == 2) {
            checkStatus = tableIns2.checkStatus('TbAuth2');
        }
        $.each(checkStatus.data, function (index, value) {
            var authid = value.authorityId;
            authArray.push(authid);
        });
        if (authArray.length < 1) {
            parent.layer.msg('请选择数据操作', {shift: 6});
            return;
        }
        $.ajax({
            type: 'POST',
            url: '<c:url value="/web/admin/systemRole/maneyAddOrRemoveAuth.action"/>',
            data: {
                roleId: $("#roleId").val(),
                authArray: authArray,
                type:type
            },
            async: false,
            success: function (result) {
                var errcode = result.errcode;
                var errmsg = result.errmsg;
                if (errcode == 0) {
                    parent.layer.msg('操作成功', {icon: 1});
                } else {
                    parent.layer.msg('操作失败', {shift: 6});
                }
                searchSubmit1();
                searchSubmit2();
            },
            error: function () {
                alert('ajax error!');
            }
        });
    }

    function searchSubmit1() {
        var searchWhere = tableRender1.config.where;
        //搜索项
        tableIns1.reload('TbAuth1', {
            where: searchWhere
        });
    }

    function searchSubmit2() {
        var searchWhere = tableRender2.config.where;
        //搜索项
        tableIns2.reload('TbAuth2', {
            where: searchWhere
        });
    }
</script>